<template>
  <a-card :bordered="false" style="margin-top: 24px" title="标签管理" class="ant-pro-components-tag-select">
    <div class="table-operator">
      <a-button type="primary" class="btn-success" icon="plus" @click="handleAdd()">新建</a-button>
    </div>
    <a-table
      :columns="columns"
      :data-source="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      rowKey="id"
    >
      <span slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">修改</a>
        <a-divider type="vertical" />
        <a @click="showDeleteModal(record)">删除</a>
      </span>
    </a-table>
    <tag-create-modal ref="tagCreateModal" @ok="handleOk" />
    <tag-edit-modal ref="tagEditModal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { sqlSelect, sqlFindOne, sqlDelete } from '@/api/sql'
import tagCreateModal from './modules/tag/createModal'
import tagEditModal from './modules/tag/editModal'
export default {
  name: 'CategoryTagList',
  components: {
    tagCreateModal,
    tagEditModal
  },
  data () {
    return {
      pagination: {
        page: 0, // 请求参数
        defaultPageSize: 10,
        pageNo: 1,
        pageSize: 10, // 默认每页显示数量
        showSizeChanger: true, // 显示可改变每页数量
        showQuickJumper: true, // 是否可以快速跳转至某页
        pageSizeOptions: ['10', '20', '50', '100'], // 每页数量选项
        showTotal: (total) => this.$t('pagination.total') + ` ${total} ` + this.$t('pagination.items'), // 分页中显示总的数据
        total: 0 // 总条数
      },
      loading: false,
      columns: [
        {
          title: 'ID',
          dataIndex: 'id',
          key: 'id'
        },
        {
          title: '标签名称',
          dataIndex: 'name',
          key: 'name'
        },
        {
          title: '操作',
          key: 'action',
          width: 160,
          scopedSlots: { customRender: 'action' }
        }
      ],
      data: []
    }
  },
  mounted () {
    this.getData(this.pagination.page, this.pagination.pageSize)
  },
  methods: {
    /**
     * 切换分页，重新请求数据
     */
    handleTableChange (pagination, filters, sorter) {
      this.pagination.pageSize = pagination.pageSize
      this.pagination.page = (pagination.current - 1) * this.pagination.pageSize
      this.getData(this.pagination.page, this.pagination.pageSize)
    },

    /**
     * 获取数据
     */
    async getData (page, size) {
      this.data = await this.getList(page, size)
      this.pagination.total = await this.getTotal()
      console.log('this.data', this.data)
      console.log(' this.pagination.total', this.pagination.total)
    },

    /**
     * 获取列表数据
     */
    getList (page, size) {
      const params = {
        sql: `select * from t_category_tag order by id desc limit ${page},${size}`
      }
      return new Promise((resolve, reject) => {
        sqlSelect(params).then(
          (res) => {
            resolve(res)
          },
          (error) => {
            console.log(error)
            resolve(error)
          }
        )
      })
    },

    /**
     * 获取数据总数
     */
    getTotal () {
      const params = {
        sql: 'select count(*) as total from t_category_tag'
      }
      return new Promise((resolve, reject) => {
        sqlFindOne(params).then(
          (res) => {
            resolve(res.total)
          },
          (error) => {
            console.log(error)
            resolve(error)
          }
        )
      })
    },

    /**
     * 修改
     */
    handleEdit (record) {
      this.$refs.tagEditModal.init(record['id'])
    },

    /**
     * 新建
     */
    handleAdd () {
      this.$refs.tagCreateModal.init()
    },

    /**
     * 删除弹框
     */
    showDeleteModal (record) {
      this.$confirm({
        title: '提示',
        content: '确定删除数据吗?',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          console.log('OK')
          this.delete(record['id']).then((res) => {
            this.$notification['success']({
              message: '提示',
              description: '数据删除成功'
            })
            // 重新请求数据
            this.getData(this.pagination.page, this.pagination.pageSize)
          })
        },
        onCancel () {
          console.log('Cancel')
        }
      })
    },

    /**
     * 删除数据
     */
    delete (id) {
      const params = {
        sql: `delete from t_category_tag where id=${id}`
      }
      return new Promise((resolve, reject) => {
        sqlDelete(params).then(
          (res) => {
            resolve(res)
          },
          (error) => {
            console.log(error)
            resolve(error)
          }
        )
      })
    },

    /**
     * 刷新数据
     */
    handleOk () {
      console.log('11111xxx')
      this.getData(this.pagination.page, this.pagination.pageSize)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
